#!/usr/bin/env bash
index_no="01"
version="5.0"
containerName="redis-dev-${index_no}-${version}"
port="16379"
password="123456@#^^^"
install_dir="/docker/redis/"
install_index_no_dir=$install_dir$index_no
conf=$install_dir$index_no/conf/
data=$install_dir$index_no/data/

echo '>>> Please later ... ...'


if [ ! -d $install_index_no_dir ];then
  mkdir -p $install_index_no_dir
else
  cd $install_index_no_dir
  rm -rf ./*
fi
echo "开始创建并复制映射目录..."
mkdir -p $install_index_no_dir/conf
mkdir -p $data
cp -f ${install_dir}redis.conf $conf
# 替换密码 sed -i "s/旧的字符串/新的字符串/g" 文件的路径
sed -i "s/requirepass 123456/requirepass ${password}/g" ${conf}redis.conf

set -e
sudo docker pull redis:$version |  grep "Image is up to date" || (echo Already up to date. Exiting... && exit 0)

echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep ${containerName} | awk '{print $1}')
echo $CID
echo ">>> Stopping and deleting old container ${containerName}"
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi

tempMountCommand="docker run --restart=always -d --privileged=true -p ${port}:6379 -v ${conf}redis.conf:/etc/redis/redis.conf -v ${data}:/data --name ${containerName} redis:5.0 redis-server /etc/redis/redis.conf --appendonly yes"
echo ">>> 执行挂载的命令： ${tempMountCommand}"
# 挂载
$tempMountCommand